#include "recorddetaildialog.h"
#include "ui_recorddetaildialog.h"
#include <QDebug>
#include <QDateTime>

RecordDetailDialog::RecordDetailDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::RecordDetailDialog)
{
    ui->setupUi(this);
    
    // 设置窗口属性
    setWindowTitle("操作记录详情");
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    setModal(true);
    resize(500, 400);
    
    // 连接关闭按钮
    connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &RecordDetailDialog::accept);
}

RecordDetailDialog::~RecordDetailDialog()
{
    delete ui;
}

// 设置要显示的记录
void RecordDetailDialog::setRecord(const StockRecord& record)
{
    m_record = record;
    loadItemDetails();
    loadRecordDetails();
}

// 加载记录详情
void RecordDetailDialog::loadRecordDetails()
{
    // 基本信息
    ui->recordIdLabel->setText(QString::number(m_record.id));
    ui->timestampLabel->setText(m_record.timestamp);
    ui->operatorLabel->setText(m_record.username);
    ui->operationTypeLabel->setText(m_record.type);
    ui->quantityLabel->setText(QString("%1 件").arg(m_record.quantity));
    
    // 根据操作类型设置颜色和图标
    if (m_record.type == "入库") {
        ui->operationTypeLabel->setStyleSheet("color: #28a745; font-weight: bold; font-size: 14px;");
        ui->operationTypeLabel->setText("✅ " + m_record.type);
    } else if (m_record.type == "出库") {
        ui->operationTypeLabel->setStyleSheet("color: #dc3545; font-weight: bold; font-size: 14px;");
        ui->operationTypeLabel->setText("📤 " + m_record.type);
    }
    
    // 物品信息
    ui->itemNameLabel->setText(m_itemInfo.name);
    ui->itemModelLabel->setText(m_itemInfo.model.isEmpty() ? "无" : m_itemInfo.model);
    ui->itemLocationLabel->setText(m_itemInfo.location);
    ui->currentStockLabel->setText(QString("%1 件").arg(m_itemInfo.quantity));
    
    // 时间解析和格式化
    QDateTime dt = QDateTime::fromString(m_record.timestamp, "yyyy-MM-dd hh:mm:ss");
    if (dt.isValid()) {
        QString formattedTime = dt.toString("yyyy年MM月dd日 hh:mm:ss dddd");
        ui->formattedTimeLabel->setText(formattedTime);
        
        // 计算时间差
        QDateTime now = QDateTime::currentDateTime();
        qint64 secondsAgo = dt.secsTo(now);
        
        QString timeAgo;
        if (secondsAgo < 60) {
            timeAgo = "刚刚";
        } else if (secondsAgo < 3600) {
            timeAgo = QString("%1分钟前").arg(secondsAgo / 60);
        } else if (secondsAgo < 86400) {
            timeAgo = QString("%1小时前").arg(secondsAgo / 3600);
        } else {
            timeAgo = QString("%1天前").arg(secondsAgo / 86400);
        }
        ui->timeAgoLabel->setText(timeAgo);
    }
    
    // 生成操作摘要
    QString summary;
    if (m_record.type == "入库") {
        summary = QString("操作员 %1 于 %2 将 %3 件 \"%4\" 入库到 %5")
                  .arg(m_record.username)
                  .arg(dt.toString("MM月dd日 hh:mm"))
                  .arg(m_record.quantity)
                  .arg(m_itemInfo.name)
                  .arg(m_itemInfo.location);
    } else {
        summary = QString("操作员 %1 于 %2 从 %3 出库了 %4 件 \"%5\"")
                  .arg(m_record.username)
                  .arg(dt.toString("MM月dd日 hh:mm"))
                  .arg(m_itemInfo.location)
                  .arg(m_record.quantity)
                  .arg(m_itemInfo.name);
    }
    ui->summaryTextEdit->setPlainText(summary);
}

// 获取物品详细信息
void RecordDetailDialog::loadItemDetails()
{
    DatabaseManager& dbManager = DatabaseManager::getInstance();
    m_itemInfo = dbManager.getItemById(m_record.itemId);
    
    if (m_itemInfo.id == 0) {
        // 物品不存在（可能已被删除）
        m_itemInfo.name = m_record.itemName + " (已删除)";
        m_itemInfo.model = "无";
        m_itemInfo.location = "无";
        m_itemInfo.quantity = 0;
    }
}